//
// Copyright (C) 2008 Jackson Harper (jackson@ximian.com)

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using Grr.Models;

namespace Grr.Web.WebControls {

	public class TagCloud : WebControl
	{
		private static string[] FontSizes = new string[] { "xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large" };

        private List<Tag> tags;

        public TagCloud() : base (HtmlTextWriterTag.Div)
        {
        }
        
        public TagCloud(List<Tag> tags) : this ()
        {
            this.tags = tags;
        }

        public List<Tag> Tags
        {
            get { return tags; }
            set { tags = value; }
        }

        protected override void CreateChildControls ()
		{
            base.CreateChildControls();
            if (tags == null)
                return;
			
            double mean;
			double stddev;
			CalculateMeanAndStandardDeviation(out mean, out  stddev);

			foreach (Tag tag in tags) {
                HtmlAnchor anchor = new HtmlAnchor();

                anchor.HRef = Tag.UrlForTag (tag.Name);
				anchor.InnerText = tag.Name;
                anchor.Attributes["rel"] = "tag";

				int norm = Normalize (tag.Instances, mean, stddev);

				anchor.Style.Add(System.Web.UI.HtmlTextWriterStyle.FontSize, FontSizes [norm - 1]);
                // anchor.Style.Add(HtmlTextWriterStyle.WhiteSpace, "nowrap");
				
                Controls.Add(anchor);
				Controls.Add(new System.Web.UI.LiteralControl (Environment.NewLine));
			}
		}

		private void CalculateMeanAndStandardDeviation (out double mean, out double stddev)
		{
			long sum = 0;
			long count = 0;
			foreach (Tag tag in tags) {
				sum += tag.Instances;
				count++;
			}

            if (count > 0)
                mean = sum / count;
            else
                mean = 0;
            
			double variance = 0;

            foreach (Tag tag in tags)
            {
				double diff = tag.Instances - mean;
				variance += diff * diff;
			}

			stddev = Math.Sqrt(variance / count);
		}

		private static int Normalize (double weight, double mean, double stddev)
		{
			double factor = (weight - mean);

			if (factor != 0 && stddev != 0)
				factor /= stddev;

			return (factor > 2) ? 7 : (factor > 1) ? 6 : (factor > 0.5) ? 5 : (factor > -0.5) ? 4 :	(factor > -1) ? 3 : (factor > -2) ? 2 :	1;
		}


	}
}
